package com.txzt.gaea.business.device.service.impl;

import com.txzt.gaea.business.device.domain.model.DeviceInfo;
import com.txzt.gaea.business.device.domain.model.DeviceUse;
import com.txzt.gaea.business.device.service.IDeviceInfoService;
import com.txzt.gaea.business.device.service.IDeviceUseService;
import com.txzt.gaea.common.base.service.impl.BaseServiceImpl;
import com.txzt.gaea.common.enums.IsDeleteEnum;
import com.txzt.gaea.common.enums.UseStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


@Service
public class DeviceUseServiceImpl extends BaseServiceImpl<DeviceUse> implements IDeviceUseService {

    @Autowired
    private IDeviceInfoService deviceInfoService;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public int addDeviceUse(DeviceUse deviceUse) {
        DeviceInfo deviceInfo = deviceInfoService.selectById(deviceUse.getDeviceId());
        if (deviceUse.getUseQty() > deviceInfo.getAvailableQty()) {
            return 99;
        }
        //更新使用库存信息
        DeviceInfo target = new DeviceInfo();
        target.setId(deviceInfo.getId());
        target.setAvailableQty(deviceInfo.getAvailableQty() - deviceUse.getUseQty());
        target.setUseQty(deviceInfo.getUseQty() + deviceUse.getUseQty());
        deviceInfoService.updateById(target);
        //新增设备领用
        deviceUse.setDeviceCode(deviceInfo.getDeviceCode());
        deviceUse.setDeviceName(deviceInfo.getDeviceName());
        deviceUse.setDeviceBrand(deviceInfo.getDeviceBrand());
        deviceUse.setDeviceModel(deviceInfo.getDeviceModel());
        deviceUse.setDeviceType(deviceInfo.getDeviceType());
        deviceUse.setDevicePrice(deviceInfo.getDevicePrice());
        deviceUse.setUseStatus(UseStatus.USING.getCode());
        deviceUse.setIsDelete(IsDeleteEnum.NOTDELETE.getCode());
        return sqlManager.insertTemplate(deviceUse);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int returnDevice(Long id) {
        DeviceUse resDeviceUse = sqlManager.single(DeviceUse.class,id);
        //更新使用库存信息
        DeviceInfo deviceInfo = deviceInfoService.selectById(resDeviceUse.getDeviceId());
        DeviceInfo target = new DeviceInfo();
        target.setId(deviceInfo.getId());
        target.setAvailableQty(deviceInfo.getAvailableQty() + resDeviceUse.getUseQty());
        target.setUseQty(deviceInfo.getUseQty() - resDeviceUse.getUseQty());
        deviceInfoService.updateById(target);
        //更新设备领用
        DeviceUse use = new DeviceUse();
        use.setId(id);
        use.setReturnTime(new Date());
        use.setUseStatus(UseStatus.RETURNED.getCode());
        return sqlManager.updateTemplateById(use);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteDeviceUse(Long id) {
        DeviceUse resDeviceUse = sqlManager.single(DeviceUse.class,id);
        //更新使用库存信息
        DeviceInfo deviceInfo = deviceInfoService.selectById(resDeviceUse.getDeviceId());
        DeviceInfo target = new DeviceInfo();
        target.setId(deviceInfo.getId());
        target.setAvailableQty(deviceInfo.getAvailableQty() + resDeviceUse.getUseQty());
        target.setUseQty(deviceInfo.getUseQty() - resDeviceUse.getUseQty());
        deviceInfoService.updateById(target);
        //删除设备领用记录
        DeviceUse deviceUse = new DeviceUse();
        deviceUse.setId(id);
        deviceUse.setIsDelete(IsDeleteEnum.DELETE.getCode());
        return sqlManager.updateTemplateById(deviceUse);
    }
}
